﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// Permissions 类。
	/// </summary>
	public static partial class Permissions
	{
		/// <summary>
		/// 获取指定的权限项。
		/// </summary>
		/// <param name="o">权限集合。</param>
		/// <param name="resourceId">资源标识。</param>
		/// <param name="permissionCode">权限码。</param>
		/// <returns>指定的权限项。</returns>
		internal static Permission GetItem(this System.Data.Linq.Table<Permission> o, Guid resourceId, int permissionCode)
		{
			return o.SingleOrDefault(p => p.ResourceId == resourceId && p.PermissionCode == permissionCode);
		}



		#region GetItemsByResourceId
		public static List<Permission> GetItemsByResourceId(Guid resourceId)
		{
			return GetItemsByResourceId(resourceId, false);
		}

		public static List<Permission> GetItemsByResourceId(Guid resourceId, bool trackObject)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = trackObject;
			return dc.Permissions.GetItemsByResourceId(resourceId);
		}

		public static List<Permission> GetItemsByResourceId(this System.Data.Linq.Table<Permission> o, Guid resourceId)
		{
			return (from p in o where p.ResourceId == resourceId select p).ToList();
		}
		#endregion
	}



	/// <summary>
	/// Permissions 类操作部分。
	/// </summary>
	public static partial class Permissions
	{
		#region Register
		/// <summary>
		/// 注册一个权限项。
		/// </summary>
		/// <param name="registerPermission">要注册的权限项。</param>
		/// <returns>注册的权限项。</returns>
		internal static Permission Register(Permission registerPermission)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			Permission permission = dc.Permissions.GetItem(registerPermission.ResourceId, registerPermission.PermissionCode);

			if (permission == null)
			{
				permission = registerPermission.ToNewEntity<Permission>();
				dc.Permissions.InsertOnSubmit(permission);
				dc.SubmitChanges();
			}
			else
			{
				if (permission.PermissionName != registerPermission.PermissionName || permission.Description != registerPermission.Description)
				{
					// 修正数据，以确保数据的正确性
					permission.PermissionName = registerPermission.PermissionName;
					permission.Description = registerPermission.Description;

					try
					{
						dc.SubmitChanges(ConflictMode.ContinueOnConflict);
					}
					catch (ChangeConflictException e)
					{
						foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
							occ.Resolve(RefreshMode.KeepCurrentValues);
					}
				}
			}

			// 返回只读数据。
			return registerPermission.FillByEntity(permission);
		}
		#endregion




		#region Delete
		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, Permission permission)
		{
			RoleContainPermissions.DeleteByParent(dc, permission);	// 删除关联数据
			dc.Permissions.DeleteOnSubmit(permission);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, Resource parent)
		{
			IEnumerable<Permission> e = parent.Permissions.AsEnumerable();
			foreach (Permission i in e)
				Delete(dc, i);
		}
		#endregion
		#endregion
	}
}